﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using PloobsEngine.MessageSystem;
using PloobsEngine.SceneControl;

namespace PloobsEngine.Audio
{
    public class SoundEmitter3D : ISoundEmitter3D
    {
        SoundEffect soundEngine;
        SoundEffectInstance soundEngineInstance;
        AudioListener listener;
        AudioEmitter emiter;
        IObject EmiterObj = null;        
        #region ISoundEmitter Members


        public SoundEmitter3D(string audioname, Vector3 Emiter)
        {
            emiter = new AudioEmitter();
            listener = new AudioListener();

            soundEngine = EngineStuff.CustomContentManager.GetAsset<SoundEffect>(audioname);
            soundEngineInstance = soundEngine.CreateInstance();

            //soundEngineInstance.IsLooped = true;
            soundEngineInstance.Volume = 1;
            emiter.Position = Emiter;
            soundEngineInstance.Apply3D(listener, emiter);
            soundEngineInstance.Play();


        }
        public SoundEmitter3D(string audioname, Vector3 Emiter, bool Loop, float volume)
        {
            emiter = new AudioEmitter();
            listener = new AudioListener();

            soundEngine = EngineStuff.CustomContentManager.GetAsset<SoundEffect>(audioname);
            soundEngineInstance = soundEngine.CreateInstance();


            soundEngineInstance.IsLooped = Loop;
            soundEngineInstance.Volume = volume;
            emiter.Position = Emiter;            

            soundEngineInstance.Apply3D(listener, emiter);
            soundEngineInstance.Play();

        }

        public SoundEmitter3D(string audioname, IObject Emiter, bool Loop, float volume)
        {
            emiter = new AudioEmitter();
            listener = new AudioListener();

            soundEngine = EngineStuff.CustomContentManager.GetAsset<SoundEffect>(audioname);
            soundEngineInstance = soundEngine.CreateInstance();

            Emiter.OnBeingRemoved += new BeingRemoved(Emiter_BeingRemoved);

            soundEngineInstance.IsLooped = Loop;
            soundEngineInstance.Volume = volume;
            emiter.Position = Emiter.Position;
            this.EmiterObj = Emiter;

            soundEngineInstance.Apply3D(listener, emiter);
            soundEngineInstance.Play();
        }

        void Emiter_BeingRemoved(IObject obj)
        {
            World.RemoveSoundEmitter(this);
        }

        public void setEmitterPosition(Vector3 position)
        {
            emiter.Position = position;
            soundEngineInstance.Apply3D(listener, emiter);
        }
        public void setListenerPosition(Vector3 position)
        {
            listener.Position = position;
            soundEngineInstance.Apply3D(listener, emiter);
        }

        void ISoundEmitter3D.Update(Microsoft.Xna.Framework.GameTime gt)
        {
            if (EmiterObj != null)
                emiter.Position = EmiterObj.Position;
        }

        public void Pause()
        {
            soundEngineInstance.Pause();
        }

        public void Resume()
        {
            soundEngineInstance.Resume();
        }

        public void Play()
        {
            soundEngineInstance.Play();
        }

        public void Stop()
        {
            soundEngineInstance.Stop();
        }

        #endregion

        #region IEntity Members
        int id;
        public int getId()
        {
            return id;
        }

        public void setId(int id)
        {
            this.id = id;
        }

        #endregion

        #region IRecieveMessageEntity Members

        public bool HandleThisMessageType(SenderType type)
        {
            return type == SenderType.SOUND;
        }

        public virtual void HandleMessage(Message mes)
        {

        }

        #endregion

        #region ISoundEmitter Members


        public SoundState State
        {
            get { return soundEngineInstance.State; }
        }

        #endregion

        #region ISoundEmitter3D Members

        public IWorld World
        {
            get;
            set;
        }

        #endregion
    }
}
